//
//  QZLoginVC.m
//  shortvideoplay
//
//  Created by tikeios_002 on 2024/6/12.
//

#import "QZLoginVC.h"
#import "HWPanModal/HWPanModal.h"
#import "YYLabel.h"
#import "NSAttributedString+YYText.h"
#import "AgreementViewController.h"
#import "CSJADLoading.h"

@interface QZLoginVC ()<UITextFieldDelegate>
@property (strong, nonatomic) YBTextFild * loginTF_name;
@property (strong, nonatomic) YBTextFild * loginTF_pass;
@property (strong, nonatomic) UIButton * loginBtn_pass;
@property (strong, nonatomic) UIButton * loginBtn_login;
@property (assign, nonatomic) int passNum;
@property (assign, nonatomic) int cutTimerNum;
@property (strong, nonatomic) NSTimer* cutTimer;
@property (strong, nonatomic) UIButton * comfitButton;
@property (strong, nonatomic) YYLabel * displayView;
@end

@implementation QZLoginVC

-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = viewback_black;
    UIButton * backButton = [[UIButton alloc]initWithFrame:CGRectMake(11, kStatusBarHeight, 44, 44)];
    [backButton setImage:KImageNamed(@"ico_nav_back_wite") forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(navBackTap) forControlEvents:UIControlEventTouchUpInside];
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:backButton];
    
    // Do any additional setup after loading the view.
    UILabel * tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 100, KScreen_W-40, 40)];
    tipLabel.textColor = [UIColor whiteColor];
    tipLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
       UILabel * oldshowSSSSTip1 = [[UILabel alloc]init];
    oldshowSSSSTip1.clipsToBounds = YES;
    oldshowSSSSTip1.text = @"kass显示区";
    oldshowSSSSTip1.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip1.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip1.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip1.hidden = YES;
    [self.view addSubview:oldshowSSSSTip1];
    [self.view addSubview:tipLabel];
    
    UILabel * subtipLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 140, KScreen_W-40, 18)];
    subtipLabel.textColor = [UIColor whiteColor];
    subtipLabel.alpha = 0.77;
    subtipLabel.font = [UIFont systemFontOfSize:14];
       UILabel * oldshowSSSSTip2 = [[UILabel alloc]init];
    oldshowSSSSTip2.clipsToBounds = YES;
    oldshowSSSSTip2.text = @"kass显示区";
    oldshowSSSSTip2.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip2.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip2.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip2.hidden = YES;
    [self.view addSubview:oldshowSSSSTip2];
    [self.view addSubview:subtipLabel];

    UIView * left_name_view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 50)];
    UILabel * codelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    codelabel.textColor = [UIColor whiteColor];
    codelabel.alpha = 0.72;
    codelabel.font = Font(15);
    codelabel.text = @"+86";
    codelabel.textAlignment = NSTextAlignmentCenter;
    [left_name_view addSubview:codelabel];
    
    UIView * left_name_line = [[UIView alloc]initWithFrame:CGRectMake(50, 17, 0.7, 16)];
    left_name_line.backgroundColor = [UIColor whiteColor];
    left_name_line.alpha = 0.72;
    [left_name_view addSubview:left_name_line];
    
    self.loginTF_name = [[YBTextFild alloc]initWithFrame:CGRectMake(20, 180, KScreen_W-40, 50)];
    self.loginTF_name.formaterType = YBAFormatterTextFieldFormatterTypePhone;
    self.loginTF_name.font = Font(15);
    self.loginTF_name.textColor = [UIColor whiteColor];
    self.loginTF_name.delegate = self;
    self.loginTF_name.keyboardType = UIKeyboardTypeNumberPad;
    self.loginTF_name.layer.cornerRadius = 8;
    self.loginTF_name.leftView = left_name_view;
    self.loginTF_name.leftViewMode = UITextFieldViewModeAlways;
    self.loginTF_name.tintColor = MainColor;
    self.loginTF_name.backgroundColor = subviewback_black;
       UILabel * oldshowSSSSTip3 = [[UILabel alloc]init];
    oldshowSSSSTip3.clipsToBounds = YES;
    oldshowSSSSTip3.text = @"kass显示区";
    oldshowSSSSTip3.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip3.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip3.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip3.hidden = YES;
    [self.view addSubview:oldshowSSSSTip3];
    [self.view addSubview:self.loginTF_name];

    self.loginTF_pass = [[YBTextFild alloc]initWithFrame:CGRectMake(20, 245, KScreen_W-180, 50)];
    self.loginTF_pass.maxLength = 4;
    self.loginTF_pass.font = Font(15);
    self.loginTF_pass.textColor = [UIColor whiteColor];
    self.loginTF_pass.keyboardType = UIKeyboardTypeNumberPad;
    self.loginTF_pass.layer.cornerRadius = 8;
    self.loginTF_pass.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 50)];
    self.loginTF_pass.leftViewMode = UITextFieldViewModeAlways;
    self.loginTF_pass.tintColor = MainColor;
    self.loginTF_pass.backgroundColor = subviewback_black;
       UILabel * oldshowSSSSTip4 = [[UILabel alloc]init];
    oldshowSSSSTip4.clipsToBounds = YES;
    oldshowSSSSTip4.text = @"kass显示区";
    oldshowSSSSTip4.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip4.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip4.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip4.hidden = YES;
    [self.view addSubview:oldshowSSSSTip4];
    [self.view addSubview:self.loginTF_pass];
        
    self.loginBtn_pass = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-145, 245, 125, 50)];
    self.loginBtn_pass.titleLabel.font = Font(15);
    [self.loginBtn_pass setTitleColor:TextColor_wite forState:UIControlStateNormal];
    [self.loginBtn_pass addTarget:self action:@selector(loginBtn_passClick) forControlEvents:UIControlEventTouchUpInside];
    self.loginBtn_pass.layer.cornerRadius = 8;
    self.loginBtn_pass.backgroundColor = MainColor;
    [self.view addSubview:self.loginBtn_pass];
    
    self.loginBtn_login = [[UIButton alloc]initWithFrame:CGRectMake(20, 315, KScreen_W-40, 50)];
    self.loginBtn_login.titleLabel.font = Font(15);
    [self.loginBtn_login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.loginBtn_login addTarget:self action:@selector(loginBtn_loginClick) forControlEvents:UIControlEventTouchUpInside];
    self.loginBtn_login.layer.cornerRadius = 8;
    self.loginBtn_login.backgroundColor = MainColor;
       UILabel * oldshowSSSSTip5 = [[UILabel alloc]init];
    oldshowSSSSTip5.clipsToBounds = YES;
    oldshowSSSSTip5.text = @"kass显示区";
    oldshowSSSSTip5.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip5.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip5.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip5.hidden = YES;
    [self.view addSubview:oldshowSSSSTip5];
    [self.view addSubview:self.loginBtn_login];
    
//        UIButton * tipLabel1 = [[UIButton alloc]initWithFrame:CGRectMake(15, KScreen_H-KTab_H-70, KScreen_W-30, 80)];
//        [tipLabel1 setTitle:NSLocalizedString(@"登录即代表同意服务和隐私条款", nil) forState:UIControlStateNormal];
//        [tipLabel1 setTitleColor:COLOR_ALPH(255, 255, 255, 0.6) forState:UIControlStateNormal];
//        [tipLabel1 setTitleColor:COLOR_ALPH(255, 255, 255, 0.4) forState:UIControlStateHighlighted];
//        tipLabel1.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];
//        [tipLabel1 addTarget:self action:@selector(Agreement) forControlEvents:UIControlEventTouchUpInside];
//        tipLabel1.hidden = YES;
//    [self.view addSubview:tipLabel1];
    
    tipLabel.text = @"登录后体验更多精彩瞬间";
    subtipLabel.text = @"未注册的手机号验证后将自动注册";
    self.loginTF_name.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:@{NSForegroundColorAttributeName:ColorHex_Alpha(@"FFFFFF", 0.72),NSFontAttributeName:Font(15)}];
    self.loginTF_pass.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSForegroundColorAttributeName:ColorHex_Alpha(@"FFFFFF", 0.72),NSFontAttributeName:Font(15)}];
    [self.loginBtn_pass setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.loginBtn_login setTitle:@"立即登录" forState:UIControlStateNormal];
    
    CGFloat left = (KScreen_W-264)/2;
    _displayView = [[YYLabel alloc] init];
    _displayView.frame = CGRectMake(left+24, 385, 240, 20);
    _displayView.textColor= [UIColor whiteColor];
    _displayView.numberOfLines = 0;
       UILabel * oldshowSSSSTip22 = [[UILabel alloc]init];
    oldshowSSSSTip22.clipsToBounds = YES;
    oldshowSSSSTip22.text = @"kass显示区";
    oldshowSSSSTip22.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip22.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip22.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip22.hidden = YES;
    [self.view addSubview:oldshowSSSSTip22];
    [self.view addSubview:_displayView];
    NSMutableAttributedString * moreAttr = [[NSMutableAttributedString alloc] initWithString:@"阅读并同意《用户协议》和《隐私政策》" attributes:@{NSForegroundColorAttributeName : ColorHex_Alpha(@"FFFFFF", 0.77), NSFontAttributeName : Font(13)}];
//    self.view.backgroundColor = ColorHex_Alpha(@"#ff1377",0.77);
    KWeakSelf
    [moreAttr setTextHighlightRange:NSMakeRange(5, 6) color:ColorHex(@"#B32651") backgroundColor:UIColor.clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        //点击
        AgreementViewController * xy = [[AgreementViewController alloc]init];
        xy.modalPresentationStyle = UIModalPresentationFullScreen;
        xy.url = [QZUserInfoModel share].agr;
        xy.Nav_title = @"用户协议";
        xy.isPerSent = YES;
        [weakSelf presentViewController:xy animated:YES completion:nil];
       
    }];
    [moreAttr setTextHighlightRange:NSMakeRange(12, 6) color:ColorHex(@"#B32651") backgroundColor:UIColor.clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        //点击
        AgreementViewController * xy = [[AgreementViewController alloc]init];
        xy.modalPresentationStyle = UIModalPresentationFullScreen;
        xy.url = [QZUserInfoModel share].pri;
        xy.Nav_title = @"隐私政策";
        xy.isPerSent = YES;
        [weakSelf presentViewController:xy animated:YES completion:nil];
    }];
    _displayView.attributedText = moreAttr;
    
    _comfitButton = [[UIButton alloc]initWithFrame:CGRectMake(left+8, 388, 14, 14)];
    [_comfitButton setImage:KImageNamed(@"ico_selete_nor") forState:UIControlStateNormal];
    [_comfitButton setImage:KImageNamed(@"ico_selete_sel") forState:UIControlStateSelected];
    [_comfitButton addTarget:self action:@selector(comfitTap) forControlEvents:UIControlEventTouchUpInside];
       UILabel * oldshowSSSSTip33 = [[UILabel alloc]init];
    oldshowSSSSTip33.clipsToBounds = YES;
    oldshowSSSSTip33.text = @"kass显示区";
    oldshowSSSSTip33.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip33.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip33.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip33.hidden = YES;
    [self.view addSubview:oldshowSSSSTip33];
    [self.view addSubview:_comfitButton];
    
    UIButton * _comfitButtonbg = [[UIButton alloc]initWithFrame:CGRectMake(left-3, 380, 30, 30)];
    [_comfitButtonbg addTarget:self action:@selector(comfitTap) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_comfitButtonbg];
    
}
- (void)comfitTap{
    _comfitButton.selected = !_comfitButton.selected;
    if(_comfitButton.selected){
        _comfitButton.alpha = 0.77;
    }else{
        _comfitButton.alpha = 1;
    }
}
- (void)loginBtn_passClick{
    [self.view endEditing:NO];
    if (self.loginTF_name.text.length == 0) {
        [YBMsgPop keyboardshowPop:@"请输入手机号"];
        return;
    }
    if (self.loginTF_name.text.length !=13) {
        [YBMsgPop keyboardshowPop:@"手机号输入错误"];
        return;
    }
    self.loginBtn_pass.userInteractionEnabled = NO;
    
    [SVProgressHUDTool showWithStatus:nil];
    KMutableDic(sendDic)
    sendDic[@"phone"] = [self.loginTF_name.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    sendDic[@"event"] = @"login";
    sendDic[@"+86"] = @"country_code";
    _passNum = 1;
    KWeakSelf
    [YBNetBase sendSMSRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        [SVProgressHUDTool dismiss];
        if(code == 200){
            [YBMsgPop showPop:NSLocalizedString(@"发送成功", nil)];
            [weakSelf.cutTimer invalidate];
            weakSelf.cutTimerNum = 90;
            [weakSelf.loginTF_pass becomeFirstResponder];
            weakSelf.cutTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doCutTimer) userInfo:nil repeats:YES];
        }else{
            [SVProgressHUDTool dismiss];
            [YBMsgPop showPop:msg];
        }
    }];
}
- (void)doCutTimer{
    _cutTimerNum -- ;
    if (_cutTimerNum <= 0) {
        [_loginBtn_pass setTitle:NSLocalizedString(@"获取验证码", nil) forState:UIControlStateNormal];
        _loginBtn_pass.userInteractionEnabled = YES;
        [_cutTimer invalidate];
    }else{
        [_loginBtn_pass setTitle:[NSString stringWithFormat:@"%dS",_cutTimerNum] forState:UIControlStateNormal];
    }
}
- (void)loginBtn_loginClick{
    if (self.loginTF_name.text.length == 0) {
        [YBMsgPop keyboardshowPop:NSLocalizedString(@"请输入手机号", nil)];
        return;
    }
    if (self.loginTF_name.text.length !=13) {
        [YBMsgPop keyboardshowPop:NSLocalizedString(@"手机号输入错误", nil)];
        return;
    }
    if (self.loginTF_pass.text.length == 0) {
        [YBMsgPop keyboardshowPop:NSLocalizedString(@"请输入验证码", nil)];
        return;
    }
    if(self.comfitButton.selected == NO){
        [self.view endEditing:YES];
        [YBMsgPop keyboardshowPop:@"请阅读并同意协议"];
        return;
    }
    [self.view endEditing:NO];
    [SVProgressHUDTool showWithStatus:nil];
    KWeakSelf
    KMutableDic(sendDic)
    sendDic[@"phone"] = [self.loginTF_name.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    sendDic[@"code"] = self.loginTF_pass.text;
    sendDic[@"source"] = @"iOS";
    sendDic[@"device_no"] = [NSString stringWithFormat:@"IOS%.2f",[[[UIDevice currentDevice] systemVersion]  floatValue]];
    [YBNetBase loginRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        [SVProgressHUDTool dismiss];
        if(code == 200){
            [QZUserInfoModel share].onceLogin = NO;
            /** 缓存数据 */
            [[NSUserDefaults standardUserDefaults]setObject:data[@"userinfo"] forKey:QZUserInfoKey];
            /** 给用户单例赋值 */
            [[QZUserInfoModel share] setValuesForKeysWithDictionary:data[@"userinfo"]];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [CSJADLoading dj_SignWithPaySecretKey];
            [YBMsgPop showPop:@"登录成功"];
            [weakSelf saveToken];
            [weakSelf jifengRequest];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"acountlogin" object:nil];
        }else{
            if(weakSelf.passNum == 0){
                [YBMsgPop showPop:@"请点击获取验证码"];
            }else{
                [YBMsgPop showPop:msg];
            }
           
        }
    }];
    
}
- (void)saveToken{
    KMutableDic(sendDic)
    sendDic[@"device_token"] = [QZUserInfoModel share].pushToken;
    sendDic[@"is_open"] = @([QZUserInfoModel share].lastPushStatus);
    [YBNetBase saveDeviceTokenRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        
    }];
}
- (void)jifengRequest{
    KMutableDic(sendDic)
    KWeakSelf
    [YBNetBase activityTaskWatchVideoInfoRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            [QZHongBaoView share].max_time = [data[@"time_length"][@"max"] intValue];
            [QZHongBaoView share].min_time = [data[@"time_length"][@"min"] intValue];
            NSLog(@"%@",data);
        }
        [weakSelf dismissViewControllerAnimated:YES completion:^{
            
        }];
    }];
}
- (void)navBackTap{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

@end
